﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestFlags.Question;

namespace LongestFlags.Answer
{
    class Effie : ILongestFlag
    {
        public struct myItem
        {
            public int Neg;
            public int Pos;
            public int Zero;
        }

        public int GetLongestFlags(int[] iArray, out int startIndex, out int endIndex)
        {
            List<myItem> Arraylist = new List<myItem>();
            List<int> counts = new List<int>();
            int i = 0;
            startIndex = 0;
            endIndex = 0;
            if (iArray.Length == 0)
                return 0;
            if (iArray.Length == 1 && iArray[0] != 0)
                return 0;
            myItem item = new myItem();
            item.Neg = 0;
            item.Pos = 0;
            item.Zero = 0;
            for (i = 0; i < iArray.Length; i++)
            {
                if (iArray[i] < 0)
                {
                    item.Neg += 1;
                }
                else if (iArray[i] > 0)
                {
                    item.Pos += 1;
                }
                else
                {
                    item.Zero += 1;
                }
            }
            Arraylist.Add(item);
            for (i = 0; i < iArray.Length - 1; i++)
            {
                if (iArray[i] < 0)
                {
                    item.Neg -= 1;
                    Arraylist.Add(item);
                }
                else if (iArray[i] > 0)
                {
                    item.Pos -= 1;
                    Arraylist.Add(item);
                }
                else
                {
                    item.Zero -= 1;
                    Arraylist.Add(item);
                }
            }
            i = 0;

            foreach (myItem tempitem in Arraylist)
            {
                int n = tempitem.Neg;
                int p = tempitem.Pos;
                int z = tempitem.Zero;
                if (n == p)
                {
                    counts.Add(n + p + z);
                    break;
                }
                else
                {
                    for (int j = Arraylist.Count - 1; j > i; j--)
                    {
                        n = tempitem.Neg - Arraylist[j].Neg;
                        p = tempitem.Pos - Arraylist[j].Pos;
                        z = tempitem.Zero - Arraylist[j].Zero;
                        if (n == p)
                        {
                            counts.Add(n + p + z);
                            break;
                        }
                        else if (n == 0 || p == 0)
                        {
                            if (z == 0)
                            {
                                counts.Add(0);
                                break;
                            }
                        }

                    }
                }
                i = i + 1;
            }
            int temp = counts[0];
            int tempindex = 0;
            for (i = 1; i < counts.Count; i++)
            {
                if (temp < counts[i])
                {
                    temp = counts[i];
                    tempindex = i;
                }
            }
            startIndex = tempindex;
            if (temp == 0)
            {
                endIndex = tempindex;
            }
            else
                endIndex = tempindex + counts[tempindex] - 1;
            return counts[tempindex];

        }
    }
}
